package com.palm.hubo2o.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.model.Issue;
import com.palm.hubo2o.util.BallUtils;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.widget.BallView;

import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dest1 on 2015/10/4.
 */
public class HisIssueAdapter extends RecyclerView.Adapter<HisIssueAdapter.IssueHolder> {
    private static final int COMMON = 0;
    private static final int K3 = 1;
    private List<Issue> issues;
    private int type;

    public HisIssueAdapter(EnumCode.gameId gameId) {
        type = gameId == EnumCode.gameId.HBP3 ? 1 : 0;
    }

    public void setIssues(List<Issue> issues) {
        this.issues = issues;
    }

    @Override
    public IssueHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        IssueHolder holder;
        View v = LayoutInflater.from(parent.getContext()).inflate(type == 0 ? R.layout.item_history : R.layout.item_history_k3, parent, false);
        holder = new IssueHolder(v);
        holder.balls = BallUtils.createBallGroup(holder.issueGroup, issues.get(0).gameId);
        return holder;
    }


    @Override
    public void onBindViewHolder(IssueHolder holder, int position) {
        final Issue issue = issues.get(position);
        holder.issueNo.setText(String.format(Locale.getDefault(), "%02d期", issue.issueNo % 100));
        List<String> balls = issue.prizeBalls.getRed();
        if (balls != null) {
            if (issue.prizeBalls.getBlue() != null)
                balls.addAll(issue.prizeBalls.getBlue());
            for (int i = 0; i < Math.min(holder.balls.size(), balls.size()); i++) {
                holder.balls.get(i).setChecked(false);
                holder.balls.get(i).setAwarded(true);
                holder.balls.get(i).setNumber(balls.get(i));
            }
        }
        if (holder.issueSum != null)
            holder.issueSum.setText(String.format(Locale.getDefault(), "和值:%d", issue.prizeBalls.getSumValue()));
        if (holder.issueType != null) {
            holder.issueType.setText(String.format(Locale.getDefault(), "%s  %s", issue.prizeBalls.getDxValue(),issue.prizeBalls.getDsValue()));
        }
    }

    @Override
    public int getItemCount() {

        return issues == null ? 0 : issues.size();
    }


    class IssueHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.his_issueNo)
        TextView issueNo;
        @Bind(R.id.his_group_balls)
        ViewGroup issueGroup;
        public List<BallView> balls;
        @Nullable
        @Bind(R.id.his_k3_sum)
        TextView issueSum;
        @Nullable
        @Bind(R.id.his_k3_type)
        TextView issueType;

        public IssueHolder(View v) {
            super(v);
            ButterKnife.bind(this, v);
        }
    }
}
